package create;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocal;
import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocalHome;
import org.multimedia.jolitorask.client.ClientLocal;
import org.multimedia.jolitorask.client.ClientLocalHome;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocalHome;

/**
 * Servlet implementation class for Servlet: AddUser
 *
 */
 public class AddUser extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

   
	private static final long serialVersionUID = 1L;
	private static final String JNDI_NAME_WRAPPER = "asteriskWrapperSessionLocal";
	private static final String JNDI_NAME_CLIENT = "clientLocal";
	private static final String JNDI_NAME = "utilisateurLocal";
	
	
	public AddUser() {
		super();
	}   	
	
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		
		    res.setContentType("text/plain");
		    PrintWriter out = res.getWriter();
		    
		    String login;
		    String pass;
		    String nom;
		    String prenom;
		    String account;
		    String email;
		    
		    /* On recupere le login */
		    if( (login=req.getParameter("login")) == null){
		    	out.println(0);
		    	return;
		    }
		    
		    /* On recupere le pass */
		    if( (pass=req.getParameter("password")) == null){
		    	out.println(0);
		    	return;
		    }
		    
		    /* On recupere le nom */
		    nom=req.getParameter("name");
		    
		    /* On recupere le password */
		    prenom=req.getParameter("firstname");
		   
		    
		    
		    /* On recupere l'email */
		    if( (account=req.getParameter("account")) == null){
		    	out.println(0);
		    	return;
		    }
		    
		    /* On recupere l'email */
		    if( (email=req.getParameter("email")) == null){
		    	out.println(0);
		    	return;
		    }
		    
		    /* Creation */
		    Context initContext = null;
		    Hashtable env = new Hashtable(1);
		    Object beanObject = null;
		    
		    	
		    try {
		    	initContext = new javax.naming.InitialContext(env);
		    	beanObject = initContext.lookup(JNDI_NAME_CLIENT);
		    	ClientLocalHome home = (ClientLocalHome) beanObject;
				
				Collection<ClientLocal> c = home.findByLogin(account);
				
				if(c == null || c.isEmpty() ){
					out.println(0);
					return;
				}
				
				ClientLocal client = c.iterator().next();
				
				/*** changer pour client.addUser() ***/
				initContext = new javax.naming.InitialContext(env);
		    	beanObject = initContext.lookup(JNDI_NAME);
		    	UtilisateurLocalHome home2 = (UtilisateurLocalHome) beanObject;
				
		    	//Objet Jolitorask
				UtilisateurLocal utilisateur = home2.create(null, login, pass, nom, prenom, client);
				//Objet Asterisk
				beanObject = initContext.lookup(JNDI_NAME_WRAPPER);
				AsteriskWrapperLocalHome homeA = (AsteriskWrapperLocalHome)beanObject;
				AsteriskWrapperLocal wrapper = homeA.create();
				wrapper.init(account, login);
				out.println(wrapper.addSuperBuddy(((Integer)utilisateur.getPrimaryKey()).intValue(), nom, prenom, pass, email));
				
			} catch (FinderException e1) {
				out.println(0);
				e1.printStackTrace();
				return;
			}
		    catch(CreateException e) {
		    	e.printStackTrace();
		    	out.println(0);
		    	return;
		    }
		    catch(SQLException e3) {
		    	e3.printStackTrace();
		    	out.println(0);
		    	return;
		    }
		    catch(NamingException e2){
		    	e2.printStackTrace();
		    	out.println(0);
		    	return;
		    }
		    
		    
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}   	  	    
}